<?php

namespace App\Common\Helper;

use App\Common\Trait\InstanceTrait;

class ApiReturnHelper
{
    use InstanceTrait;

    //返回提示语
    private string $returnMessage = 'system error';
    //返回码
    private int $returnCode = 500;
    //返回数据列表
    private mixed $returnInfo = [];
    //异步执行任务
    private $backgroundTask = null;


    private function setReturnMessage(string $message)
    {
        $this->returnMessage = $message;
        return $this;
    }

    private function setReturnCode(int $code)
    {
        $this->returnCode = $code;
        return $this;
    }

    private function setReturnInfo(mixed $data = [])
    {
        $this->returnInfo = $data;
        return $this;
    }

    public function setBackgroundTask($backgroundTask)
    {
        $this->backgroundTask = $backgroundTask;
        return $this;
    }

    /**
     * 成功返回操作，已默认code=200
     * @param $message
     * @param $info
     * @return \Illuminate\Http\JsonResponse|null
     * @author 我只想看看蓝天 <1207032539@qq.com>
     */
    public function successResponse(string $message = 'success', mixed $info = [])
    {
        return $this->setReturnCode(200)->setReturnMessage($message)->setReturnInfo($info)->returnDo();
    }

    /**
     * 资源创建成功返回
     * @param $message
     * @param $info
     * @return \Illuminate\Http\JsonResponse|null
     * @author 我只想看看蓝天 <1207032539@qq.com>
     */
    public function createdResponse(string $message = 'success', mixed $info = [])
    {
        return $this->setReturnCode(201)->setReturnMessage($message)->setReturnInfo($info)->returnDo();
    }

    /**
     * 资源删除后无内容返回
     * @return \Illuminate\Http\JsonResponse|null
     * @author 我只想看看蓝天 <1207032539@qq.com>
     */
    public function noContentResponse()
    {
        return $this->setReturnCode(204)->returnDo();
    }

    /**
     * 常规失败返回操作，已默认code=400
     * @param $message
     * @param $info
     * @return \Illuminate\Http\JsonResponse|null
     * @author 我只想看看蓝天 <1207032539@qq.com>
     */
    public function errorResponse(string $message = 'error', mixed $info = [])
    {
        return $this->setReturnCode(400)->setReturnMessage($message)->setReturnInfo($info)->returnDo();
    }

    /**
     * 通用返回
     * @param $code
     * @param $message
     * @param $info
     * @return \Illuminate\Http\JsonResponse|null
     * @author 我只想看看蓝天 <1207032539@qq.com>
     */
    public function commonResponse(int $code = 200, string $message = 'success', mixed $info = [])
    {
        return $this->setReturnCode($code)->setReturnMessage($message)->setReturnInfo($info)->returnDo();
    }

    /**
     * exit方式返回数据
     * @param $code
     * @param $message
     * @param $info
     * @return \Illuminate\Http\JsonResponse|null
     * @author 我只想看看蓝天 <1207032539@qq.com>
     */
    public function exitResponse(int $code = 500, string $message = 'error', mixed $info = [])
    {
        return $this->setReturnCode($code)->setReturnMessage($message)->setReturnInfo($info)->returnDo();
    }

    /**
     * 返回接口数据
     * @return \Illuminate\Http\JsonResponse|void
     * @author 我只想看看蓝天 <1207032539@qq.com>
     */
    private function returnDo()
    {
        //设置返回内容
        $return = [
            'message' => $this->returnMessage,
            'code' => $this->returnCode,
            'info' => $this->returnInfo
        ];

        if (strpos(php_sapi_name(), 'cli') === false) {
            //设置http返回码
            if (!in_array($this->returnCode, [204])) {
                response()->json($return, $this->returnCode, [], 256)->send();
            } else {
                http_response_code($this->returnCode);
            }
            //请求终止返回
            fastcgi_finish_request();
            //执行投递的异步后台任务
            if (is_callable($this->backgroundTask)) {
                $backgroundTask = $this->backgroundTask;
                $backgroundTask();
            }
            exit();
        }

        return response()->json($return, $this->returnCode, [], 256);
    }
}
